//
//  VVHomeTableViewController.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/26.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit
import YYModel

class VVHomeTableViewController: VVVisitorTableViewController {

    //懒加载控件
    //上拉加载图片
    fileprivate lazy var pullUpView: UIActivityIndicatorView = {
       
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
        
        //指定颜色
        indicatorView.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        
        return indicatorView
        
    }()
    
    //下拉刷新视图
//    fileprivate lazy var pullDownView: UIRefreshControl = {
//        
//        let refreshCtr = UIRefreshControl()
//        
//        refreshCtr.addTarget(self, action: #selector(pullDownRefreshAction), for: .valueChanged)
//        
//        return refreshCtr
//    }()
    
    //自定义下拉刷新控件
    fileprivate lazy var pullDownView: VVRefreshControl = {
        
        let refreshCtr = VVRefreshControl()
        
        refreshCtr.addTarget(self, action: #selector(pullDownRefreshAction), for: .valueChanged)
        
        return refreshCtr
    }()
    
    //tiplabel
    fileprivate lazy var tipLabel: UILabel = {
        
        let label = UILabel(fonSize: 12, textColor: UIColor.white, text: "没有加载到最新的微博数据")
        
        label.backgroundColor = UIColor.orange
        
        label.textAlignment = .center
        
        //默认情况下隐藏
        label.isHidden = true
        
        return label
        
    }()
    
    //引用viewModel
    fileprivate lazy var homeViewModel: VVHomeViewModel = VVHomeViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if  !isLogin {
            
         visitorView?.updateVisitorInfo(imageName: nil, message: nil)
        
        } else {
            
            setupUI()
            
            //登录成功
            loadData()
            
            
        }
  
    }
    
    fileprivate func setupUI() {
        
        if  let nav = self.navigationController {
            
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
       
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.width, height: 35)
            
        }
        
        setupTableView()
    }
    
    //tableView相关操作
    fileprivate func setupTableView() {
        
        //注册cell
        tableView.register(VVHomeTableViewCell.self, forCellReuseIdentifier: Homecellid)
        
        //tableView的行高
        //tableView.rowHeight = 200
        
        //自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        
        //预估行高,越接近真是高度越好
        tableView.estimatedRowHeight = 200
        
        //不显示分割线
        tableView.separatorStyle = .none
        
        //设置tableView的footerView
        tableView.tableFooterView = pullUpView
        
        //添加下拉刷新1
        //self.refreshControl = pullDownView
        
        //添加下拉刷新2
        tableView.addSubview(pullDownView)
    
    }
    
    //下拉刷新
    @objc private func pullDownRefreshAction() {
        
        
        loadData()
        
    }
    
    //tip动画的方法
    fileprivate func tipAnimation(message: String) {
        
        //如果正在显示表示在执行动画,然后让其返回
        if tipLabel.isHidden == false {
            
            return
        }
        
        //执行动画显示tiplabel
        tipLabel.isHidden = false
        
        //设置最新的显示内容
        tipLabel.text = message
        
        UIView.animate(withDuration: 1, animations: { 
            
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: self.tipLabel.height)
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: { 
                self.tipLabel.transform = CGAffineTransform.identity
                
            }, completion: { (_) in
                
                //动画执行完成改成不显示
                self.tipLabel.isHidden = true
            })
        }
    }
    
    //获取首页数据
    fileprivate func loadData() {

        homeViewModel.requestHomeData(isPullup:pullUpView.isAnimating ) { (isSuccess, message) in
            
            //下拉刷新的时候和第一次获取微博数据的时候执行tip动画
            if self.pullUpView.isAnimating == false {
                
                //执行动画
                self.tipAnimation(message: message)
            }
            //结束刷新
            self.endRefreshing()
            
            if isSuccess {
                
                self.tableView.reloadData()
            }
        }
    }
    
    //结束刷新
    fileprivate func endRefreshing() {
        
        //停止风火轮
        pullUpView.stopAnimating()
        
        //停止下拉刷新
        pullDownView.endRefreshing()
        
    }
    
}

//数据源方法
extension VVHomeTableViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return homeViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: Homecellid, for: indexPath) as! VVHomeTableViewCell
        
        let statusViewModel = homeViewModel.statusList[indexPath.row]
        cell.statusViewModel = statusViewModel
        
        //设置cell的选中样式
        cell.selectionStyle = .none
        return cell
    }
    
    //监听将要显示的cell
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        //滚动到最后一个cell ,没有开启动画才能加载更多数据
        if indexPath.row == homeViewModel.statusList.count - 1 && !pullUpView.isAnimating {
            
            //开启风火轮
            pullUpView.startAnimating()
            loadData()
        }
    }
    
}

